﻿///////////////////////////////////////////////////////
//
// XDesigner.ORM 
//
// autohr : yuans
//
// date: 2011-2-23
//
// email  : yyf9989@hotmail.com
// 
//
///////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace XDesigner.ORM
{
    public class ORMPerformanceCounter
    {
        public static System.Data.DataTable GetInstancesStatus()
        {
            System.Data.DataTable table = new System.Data.DataTable();
            table.Columns.Add("TypeName", typeof(string));
            table.Columns.Add("Create", typeof(int));
            table.Columns.Add("Insert", typeof(int));
            table.Columns.Add("Delete", typeof(int));
            table.Columns.Add("Update", typeof(int));
            table.Columns.Add("Buffered", typeof(int));
            foreach (ORMTypeCounter counter in _TypeCounters.Values)
            {
                System.Data.DataRow row = table.NewRow();
                row[0] = counter.TypeName;
                row[1] = counter.Created;
                row[2] = counter.Insert;
                row[3] = counter.Delete;
                row[4] = counter.Update;
                row[5] = counter.Buffered;
                table.Rows.Add(row);
            }
            return table;
        }

        internal static void IncreaseCreate(Type type)
        {
            ORMTypeCounter counter = GetCounter(type );
            counter.Created ++;
        }

        internal static void IncreaseInsert(Type type)
        {
            GetCounter(type).Insert++;
        }

        internal static void IncreaseUpdate(Type type)
        {
            GetCounter(type).Update++;
        }

        internal static void IncreaseDelete(Type type)
        {
            GetCounter(type).Delete++;
        }

        internal static void IncreaseDelete(Type type, int deleted)
        {
            GetCounter(type).Delete += deleted;
        }

        internal static void IncreaseBuffered(Type type)
        {
            GetCounter(type).Buffered ++ ;
        }

        private static ORMTypeCounter GetCounter(Type type )
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            string typeName = type.Name;
            lock (_TypeCounters)
            {
                if (_TypeCounters.ContainsKey(typeName))
                {
                    return _TypeCounters[typeName];
                }
                else
                {
                    ORMTypeCounter counter = new ORMTypeCounter();
                    counter.TypeName = typeName;
                    _TypeCounters[typeName] = counter;
                    return counter;
                }
            }
        }

        private static Dictionary<string, ORMTypeCounter> _TypeCounters = 
            new Dictionary<string, ORMTypeCounter>();

        public class ORMTypeCounter
        {
            private volatile string _TypeName = null;

            public string TypeName
            {
                get { return _TypeName; }
                set { _TypeName = value; }
            }

            private volatile int _Created = 0;

            public int Created
            {
                get { return _Created; }
                set { _Created = value; }
            }

            private volatile int _Buffered = 0;

            public int Buffered
            {
                get { return _Buffered; }
                set { _Buffered = value; }
            }

            private volatile int intInsert = 0;

            public int Insert
            {
                get { return intInsert; }
                set { intInsert = value; }
            }

            private volatile int intUpdate = 0;

            public int Update
            {
                get { return intUpdate; }
                set { intUpdate = value; }
            }

            private volatile int intDelete = 0;

            public int Delete
            {
                get { return intDelete; }
                set { intDelete = value; }
            }
        }


        public const string Name_Instance = "Instance";
        public const string Name_ExecuteCommand = "ExecuteCommand";

        private static Dictionary<string, long> myCounter = new Dictionary<string, long>();

        public static long Increment( string name )
        {
            long v = 1;
            if (myCounter.ContainsKey(name))
            {
                v = myCounter[name];
                v++;
                myCounter[name] = v;
            }
            else
            {
                myCounter[name] = v;
            }
            return v;
        }

        public static long GetValue(string name)
        {
            if (myCounter.ContainsKey(name))
                return myCounter[name];
            else
                return 0;
        }
    }
}
